package com.nidu.demo.tenant.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.tenant.convertor.TenantPackageConvertor;
import com.nidu.demo.tenant.dto.TenantPackageCO;
import com.nidu.demo.tenant.gateway.TenantPackageGateway;
import com.nidu.demo.tenant.model.TenantPackage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 租户套餐单查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class TenantPackageSingleQryExe {

    private final TenantPackageGateway gateway;

    private final TenantPackageConvertor convertor;

    public SingleResponse<TenantPackageCO> execute(Long id) {
        // 1. 查询数据
        TenantPackage tenantPackage = gateway.getById(id);
        if(tenantPackage == null){
            throw new BizException(ErrorCodeConstants.TENANT_PACKAGE_NOT_FOUND.getCode(),
                    ErrorCodeConstants.TENANT_PACKAGE_NOT_FOUND.getMessage());
        }
        // 2. 转换为DTO返回
        TenantPackageCO tenantPackageCO = convertor.toClientObject(tenantPackage);
        return SingleResponse.of(tenantPackageCO);
    }

    public SingleResponse<TenantPackageCO> execute(String name) {
        // 1. 查询数据
        TenantPackage tenantPackage = gateway.getByName(name);
        if(tenantPackage == null){
            throw new BizException(ErrorCodeConstants.TENANT_PACKAGE_NOT_FOUND.getCode(),
                    ErrorCodeConstants.TENANT_PACKAGE_NOT_FOUND.getMessage());
        }
        // 2. 转换为DTO返回
        TenantPackageCO tenantPackageCO = convertor.toClientObject(tenantPackage);
        return SingleResponse.of(tenantPackageCO);
    }
}
